package com.chat.datea.adapter;

import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chat.datea.R;
import com.chat.datea.bean.RecomNewsMusicEntivity;
import com.chat.datea.bean.RecomNewsToDayMusicEntivity;
import com.chat.datea.fragment.AlbumInfoFragment;
import com.chat.datea.fragment.TodayNewSongFragment;
import com.chat.datea.utils.fragment.ReplaceFragment;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by xyb on 15/10/21.
 */
public class MusicRecomFragment_GridView_Adapter extends BaseAdapter {
    private Context context;
    private List<RecomNewsMusicEntivity.PlazeAlbumListEntity.RMEntity.AlbumListEntity.ListEntity> listEntities;
    private RecomNewsToDayMusicEntivity.CollectionEntity entity;
    private FragmentManager fManager;


    public MusicRecomFragment_GridView_Adapter(Context context, FragmentManager fManager) {
        this.context = context;
        this.fManager = fManager;

    }

    public void addTodayData(RecomNewsToDayMusicEntivity.CollectionEntity entity) {
        this.entity = entity;
        notifyDataSetChanged();
    }

    public void addNewsData(List<RecomNewsMusicEntivity.PlazeAlbumListEntity.RMEntity.AlbumListEntity.ListEntity> listEntities) {
        this.listEntities = listEntities;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public Object getItem(int position) {
        return 3;
    }

    @Override
    public long getItemId(int position) {
        return 3;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = View.inflate(context, R.layout.musicrecom_gridview_item, null);
            viewHolder.gone = (ImageView) convertView.findViewById(R.id.gone);
            viewHolder.music_img = (ImageView) convertView.findViewById(R.id.music_img);
            viewHolder.music_name = (TextView) convertView.findViewById(R.id.music_name);
            viewHolder.music_singer = (TextView) convertView.findViewById(R.id.music_singer);
            viewHolder.dujia = (ImageView) convertView.findViewById(R.id.dujia);
            viewHolder.layout = (RelativeLayout) convertView.findViewById(R.id.music_re);
            viewHolder.img_press = (ImageView) convertView.findViewById(R.id.img_press);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (position == 0) {
            if (entity != null) {
                viewHolder.gone.setVisibility(View.GONE);
                viewHolder.dujia.setVisibility(View.GONE);
                ViewGroup.LayoutParams params = viewHolder.layout.getLayoutParams();
                params.height = 222;
                params.width = 222;
                viewHolder.layout.setLayoutParams(params);
                Picasso.with(context).load(entity.getPic()).into(viewHolder.music_img);
                viewHolder.music_name.setText(entity.getTitle());
                viewHolder.music_singer.setText(entity.getMessage());
                viewHolder.img_press.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String list_id = entity.getList_id();
                        TodayNewSongFragment newSongFragment = new TodayNewSongFragment(list_id);
                        new ReplaceFragment(newSongFragment, fManager, R.id.fl_content, true, null);

                    }
                });
            }
        } else {
            if (listEntities != null) {
                Picasso.with(context).load(listEntities.get(position - 1).getPic_radio()).into(viewHolder.music_img);
                viewHolder.music_name.setText(listEntities.get(position - 1).getTitle());
                viewHolder.music_singer.setText(listEntities.get(position - 1).getAuthor());
                if (!listEntities.get(position - 1).getIs_first_publish().equals("1")) {
                    viewHolder.dujia.setVisibility(View.GONE);
                }
                viewHolder.img_press.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        String album_id = listEntities.get(position - 1).getAlbum_id();
                        AlbumInfoFragment albumInfoFragment = new AlbumInfoFragment(album_id);
                        new ReplaceFragment(albumInfoFragment, fManager, R.id.fl_content, true, null);

                    }
                });
            }
        }
        return convertView;
    }

    private class ViewHolder {
        private ImageView gone, music_img, dujia, img_press;
        private TextView music_name, music_singer;
        private RelativeLayout layout;

    }
}
